package com.lstimeline.servlet;

import java.io.IOException;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.lstimeline.utility.DataManager.ViewDataManager;
import com.lstimeline.bean.*;

public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// a hashmap of school ids to schools' names
	private static HashMap<Integer, String> schoolMap;
	// A school list for display
	private static ArrayList<SchoolBean> schoolList;
	
	@SuppressWarnings("unchecked")
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		ViewDataManager.loadDriver();
		
		//school list should be generated only once
		ServletContext context = getServletContext();
		synchronized(this) {
			if (context.getAttribute("schoolMap") == null) {
				HashMap<Integer, String> schoolMap = ViewDataManager.getSchoolNameMap();
				context.setAttribute("schoolMap", schoolMap);
				//System.out.println("School map created");
			}
			
			schoolMap = (HashMap<Integer, String>) context.getAttribute("schoolMap");
			
			schoolList = new ArrayList<SchoolBean>(schoolMap.size());
			
			Iterator<Integer> it = schoolMap.keySet().iterator();
			while(it.hasNext()) {
				int schoolid = it.next();
				String schoolname = schoolMap.get(schoolid);
				
				SchoolBean sb = new SchoolBean();
				sb.setSchoolid(schoolid);
				sb.setFullname(schoolname);
				
				schoolList.add(sb);
			}
			//ensure that schools are listed alphabatically
			Collections.sort(schoolList);
			//System.out.println("School list created");
		}
	}
	
	/**
	 * Get request will be forwarded to the initial school selection page
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
		//some get parameter reading
		//default case: home page
		RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/home.jspx");
		dispatcher.forward(request, response);
	}
	
	/**
	 * Post request will be handled to generate input fields
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
		ArrayList<SchoolBean> selectedSchools = new ArrayList<SchoolBean>();
		
		Iterator<Integer> it = schoolMap.keySet().iterator();
		while(it.hasNext()) {
			//checkbox mode
			int schoolid = it.next();
			if(request.getParameter("school_" + schoolid) != null) {
				SchoolBean sb = new SchoolBean();
				sb.setSchoolid(schoolid);
				sb.setFullname(schoolMap.get(schoolid));
				
				selectedSchools.add(sb);
			}
		}
		
		//shouldn't happen normally, only if no school is selected
		if(selectedSchools.size() == 0) {
			request.setAttribute("errorMessage", "Please select at least one school.");
			doGet(request, response);
		} else {
			Collections.sort(selectedSchools);
			
			request.setAttribute("selectedSchools", selectedSchools);
			RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/enter_credentials.jspx");
			dispatcher.forward(request, response);
		}
	}
	
}
